package com.dzsw.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @author sunz
 * @version 1.0
 * @CreateTime 2021-05-11-11:51
 */
@Slf4j
@Aspect
@Component
@Order(7)
public class Neo4jAspect {

    private int num = 0;

    public void printGood() {
        System.out.println("good --- " + num);
    }


    /**
     * 这里我们使用注解的形式
     * 当然，我们也可以通过切点表达式直接指定需要拦截的package,需要拦截的class 以及 method
     * 切点表达式:   execution(...)
     */
    @Pointcut("@annotation(com.dzsw.aspect.Neo4j)")
    public void demoCut() {
    }


    @Around("demoCut()")
//    @Around("execution(public * com.dzsw.service..*.*(..)) && @annotation(com.dzsw.aspect.Neo4j)")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        long startTime = System.currentTimeMillis();
        try {
            log.info("====================== THIS IS BEFORE ======================");
            // 打开
            Object[] args = point.getArgs();
            args[0] = "ssssss";
            Object result = point.proceed(args);
            long useTime = System.currentTimeMillis() - startTime;
            log.info(String.format("Total use time: %s", useTime));
            return result;
            // 提交
        } catch (Throwable e) {
            // 回滚
            throw e;
        } finally {
            // TODO 关闭数据库
            log.info("====================== THIS IS AFTER ======================");
        }
    }


//    @Before("demoCut()")
//    public void before(JoinPoint point) throws Throwable {
//        log.info("====================== THIS IS BEFORE ======================");
//    }
//
//    @After("demoCut()")
//    public void after(JoinPoint point) throws Throwable {
//        log.info("====================== THIS IS AFTER ======================");
//    }
}
